﻿using System;
using System.Windows.Input;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Resources;

namespace BaseControls
{
    public class HitScore
    {
        public int goodClick = 0;
        public int badClick = 0;
    }
    /// <summary>
    /// Base class for any HOG levels.
    /// </summary>
    public class HogBaseLevel : BaseLevel
    {
        /// <summary>
        /// This variable have user clicks on items that he have to find.
        /// </summary>
        private int goodClick = 0;
        /// <summary>
        /// This variable have user clicks on levels not items.
        /// </summary>
        private int badClick = 0;        
        private MediaElement SoundPlayer = null;
        public HogBaseLevel()
        {
            base.MouseLeftButtonUp += new MouseButtonEventHandler(HogBaseLevel_MouseLeftButtonUp);            
            SoundPlayer = new MediaElement();             
        }        
        public static readonly DependencyProperty NextLevelSoundProperty = DependencyProperty.Register("NextLevelSound", typeof(string), typeof(HogBaseLevel), null);
        public string NextLevelSound
        {
            get;
            set;
        }
        /// <summary>
        /// Property that define sound when user click item
        /// </summary>
        public static readonly DependencyProperty GoodClickSoundProperty = DependencyProperty.Register("GoodClickSound", typeof(string), typeof(HogBaseLevel), null);       
        public string GoodClickSound
        {
            get;
            set;
        }
        /// <summary>
        /// Property that define sound when user click on level (not item)
        /// </summary>
        public static readonly DependencyProperty BadClickSoundProperty = DependencyProperty.Register("BadClickSound", typeof(string), typeof(HogBaseLevel), null);
        public string BadClickSound
        {
            get;
            set;
        }
        /// <summary>
        /// Property that define volume of music for level
        /// </summary>
        public static readonly DependencyProperty LevelMusicVolumeProperty = DependencyProperty.Register("LevelMusicVolume", typeof(double), typeof(HogBaseLevel), new PropertyMetadata((double)0.5));        
        public double LevelMusicVolume
        {
            get
            {                
                return (double)this.GetValue(LevelMusicVolumeProperty);
            }
            set
            {                
                this.SetValue(LevelMusicVolumeProperty,value);
            }
        }
        void HogBaseLevel_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (e.OriginalSource == this)
               OnItemComplete(null,sender, e);
        }                   
        public override void  Refresh()
        {
            base.Refresh();   
        }                
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();       
        }
        /// <summary>
        /// This method represent main hog level logic.
        /// </summary>
        /// <param name="data">Data for method</param>
        /// <param name="sender">What item raise the event</param>
        /// <param name="arg">Arguments</param>
        public override void OnItemComplete(object data,object sender, EventArgs arg)
        {
            if(sender != null)
                ObjectList.Remove(sender);
            if (sender is HogBaseLevel)
            {
                badClick++;
                LoadSound(BadClickSound);                
                return;                
            }
            else
            {
                goodClick++;
                LoadSound(GoodClickSound);                              
                if (base.ObjectList.Count == 0)
                {
                    FinishLevel(new HitScore() { goodClick = goodClick, badClick = badClick });
                }
                else
                    OnGameEvent(data, GameChangeType.ItemComplete);                                
            }                           
        }
        protected override void FinishLevel(object data)
        {
            base.FinishLevel(data);
            SoundPlayer.Stop();            
            LoadSound(NextLevelSound);
            StopMusic();
        }
        public override void PlayMusic()
        {
            if(LevelMusicVolume != 0.0)
            {                
                BackgroundMusicPlayer.Volume = LevelMusicVolume;
                base.PlayMusic();
            }            
        }
        public override void UpdateMusic()
        {
            if (BackgroundMusicPlayer != null)
                BackgroundMusicPlayer.Volume = LevelMusicVolume;
            base.UpdateMusic();
        }
        private void LoadSound(string uri)
        {
            if (uri == null || uri == "" || LevelMusicVolume == 0.0)
                return;
            StreamResourceInfo info = Application.GetResourceStream(new Uri(uri, UriKind.RelativeOrAbsolute));
            if (info != null)//if sound is in resource
                SoundPlayer.SetSource(info.Stream);
            else
                SoundPlayer.Source = new Uri(uri, UriKind.RelativeOrAbsolute);                        
            SoundPlayer.Volume = LevelMusicVolume;                
            SoundPlayer.Play();
        }
    }
}
